package com.ruoyi.system.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.system.domain.Exam;
import com.ruoyi.system.domain.PaperSubject;
import com.ruoyi.system.domain.vo.StudentSubjectVo;
import com.ruoyi.system.mapper.ExamMapper;
import com.ruoyi.system.mapper.PaperSubjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.StudentSubjectMapper;
import com.ruoyi.system.domain.StudentSubject;
import com.ruoyi.system.service.IStudentSubjectService;

/**
 * 学生考题表Service业务层处理
 *
 * @author ruoyi
 * @date 2022-11-20
 */
@Service
public class StudentSubjectServiceImpl implements IStudentSubjectService
{
    @Autowired
    private StudentSubjectMapper studentSubjectMapper;

    @Autowired
    private ExamMapper examMapper;

    @Autowired
    private PaperSubjectMapper paperSubjectMapper;
    /**
     * 查询学生考题表
     *
     * @param studentId 学生考题表主键
     * @return 学生考题表
     */
    @Override
    public StudentSubject selectStudentSubjectByStudentId(Long studentId)
    {
        return studentSubjectMapper.selectStudentSubjectByStudentId(studentId);
    }

    /**
     * 查询学生考题表列表
     *
     * @param studentSubject 学生考题表
     * @return 学生考题表
     */
    @Override
    public List<StudentSubject> selectStudentSubjectList(StudentSubject studentSubject)
    {
        return studentSubjectMapper.selectStudentSubjectList(studentSubject);
    }

    /**
     * 新增学生考题表
     *
     * @return 结果
     */
    @Override
    public int insertStudentSubject(StudentSubjectVo studentSubjectVo)
    {
//      转化考试序号为考试id
        Long subjectNow = studentSubjectVo.getSubjectId()-1;
        Exam exam = examMapper.selectExamByExamId(studentSubjectVo.getExamId());
        Long paperId = exam.getPaperId();
        PaperSubject paperSubject=new PaperSubject();
        paperSubject.setPaperId(paperId);
        paperSubject.setSubjectNow(String.valueOf(subjectNow));
        List<PaperSubject> subjectList = paperSubjectMapper.selectPaperSubjectList(paperSubject);
        Long subjectId = subjectList.get(0).getSubjectId();
        studentSubjectVo.setSubjectId(subjectId);

        Date date=new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:ss:mm.SSS");
        Date endTime = examMapper.selectExamByExamId(studentSubjectVo.getExamId()).getEndTime();
        Long i =(date.getTime() - endTime.getTime())/1000/60;
        if (i>0){
            return -12;
        }
        StudentSubject studentSubject=new StudentSubject();
        studentSubject.setStudentId(studentSubjectVo.getStudentId());
        studentSubject.setExamId(studentSubjectVo.getExamId());
        int t=1;
        if(studentSubjectMapper.selectStudentSubjectList(studentSubject).get(0)!=null){
            t=1;
        }
        studentSubject.setSubjectId(studentSubjectVo.getSubjectId());
        studentSubject.setAnswer(studentSubjectVo.getAnswer());
        studentSubject.setSubmitTime(date);
        if (t==1){
            return studentSubjectMapper.updateStudentSubject(studentSubject);
        }
        return studentSubjectMapper.insertStudentSubject(studentSubject);
    }

    /**
     * 修改学生考题表
     *
     * @param studentSubject 学生考题表
     * @return 结果
     */
    @Override
    public int updateStudentSubject(StudentSubject studentSubject)
    {
        return studentSubjectMapper.updateStudentSubject(studentSubject);
    }

    /**
     * 批量删除学生考题表
     *
     * @param studentIds 需要删除的学生考题表主键
     * @return 结果
     */
    @Override
    public int deleteStudentSubjectByStudentIds(Long[] studentIds)
    {
        return studentSubjectMapper.deleteStudentSubjectByStudentIds(studentIds);
    }

    /**
     * 删除学生考题表信息
     *
     * @param studentId 学生考题表主键
     * @return 结果
     */
    @Override
    public int deleteStudentSubjectByStudentId(Long studentId)
    {
        return studentSubjectMapper.deleteStudentSubjectByStudentId(studentId);
    }
}
